package com.hjq.mallorder.web;

import com.hjq.mallorder.entity.OrderEntity;
import com.hjq.mallorder.entity.vo.OrderConfirmVO;
import com.hjq.mallorder.entity.vo.OrderResponseVO;
import com.hjq.mallorder.entity.vo.OrderSubmitVO;
import com.hjq.mallorder.service.OrderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @auther HJQ
 */
@Controller
public class OrderWebController {
    @Autowired
    OrderService orderService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/toTrade")
    public String toTrade(Model model){
        OrderConfirmVO orderConfirmVO= orderService.getOrder();
        model.addAttribute("orderConfirm",orderConfirmVO);
        System.out.println(orderConfirmVO);

        return "confirm";
    }
    @RequestMapping("/submitOrder")
    public String submitOrder(OrderSubmitVO orderSubmitVO,Model model) {
        OrderResponseVO responseVO = orderService.submitOrder(orderSubmitVO);
        if (responseVO.getCode() != 0) {
            //失败去订单提交页
            return "redirect:http://order.hjqmall.com/toTrade";
        }
        model.addAttribute("orderSubmit",responseVO);
        //成功去支付页
        return "pay";
    }
@ResponseBody
    @RequestMapping("/createOrder")
    public String createOrder(OrderSubmitVO orderSubmitVO,Model model) {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn("1234");
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",orderEntity);
        return "good";
    }
}
